<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/12 14:38
 * Modified Time: 2019/7/12 14:38
 * Version: 2.0
 */

namespace App\Modules\Cashier\Repositories;


use App\Exceptions\AllValidatorException;
use App\Exceptions\NotFoundException;
use App\Models\Bill;
use Flower\Core\Repository\Eloquent\BaseRepository;

class BillRepository extends BaseRepository
{
	public function model()
	{
		return Bill::class;
	}

	/**
	 * 新增账单表
	 * @param $category
	 * @param $order_id
	 * @param $store_id
	 * @param $amount
	 * @param $discount_amount
	 * @param $receipts
	 * @param $member_id
	 * @param $user_id
	 * @param $status
	 * @return mixed
	 */
	public function createBill($category, $order_id, $store_id, $amount, $discount_amount, $receipts, $member_id, $user_id, $status, $main_recommend, $assist_recommend)
	{
		$data = [
			'category'         => $category,
			'order_id'         => $order_id,
			'store_id'         => $store_id,
			'amount'           => $amount,
			'discount_amount'  => $discount_amount,
			'receipts'         => $receipts,
			'member_id'        => $member_id,
			'user_id'          => $user_id,
			'status'           => $status,
			'main_recommend'   => $main_recommend,
			'assist_recommend' => $assist_recommend
		];
		return $this->model->insertGetId($data);
	}

	/**
	 * 更新主的订单表的状态
	 * @param $billId
	 * @param $amount
	 * @param $receipts
	 * @param int $status
	 * @return mixed
	 */
	public function updateBillByCategoryAndOrderId($billId, $amount, $receipts, $status = 1)
	{
		$discount_amount = isset($amount) && isset($receipts) ? round($receipts - $amount, 2) : 0;
		return $this->update(['amount' => $amount, 'discount_amount' => $discount_amount, 'receipts' => $receipts, 'status' => $status, 'finish_time' => date('Y-m-d H:i:s')], $billId);

	}


	/**
	 * 检测是否存在
	 * @param $id
	 * @return mixed
	 * @throws NotFoundException
	 */
	public function isExited($id)
	{
		$billInfo = $this->find($id);
		if (empty($billInfo)) {
			throw  new NotFoundException(['msg' => '账单不存在，请刷新！']);
		}
		return $billInfo;
	}

	/**
	 * 账单是否完成支付
	 * @param $id
	 * @return bool
	 * @throws AllValidatorException
	 * @throws NotFoundException
	 */
	public function isNotPay($id)
	{
		$billInfo = $this->isExited($id);
		if ($billInfo->status == 1) {
			throw  new AllValidatorException(['msg' => '该账单已经完成，请勿重复操作！']);
		}
		return $billInfo;
	}



	public function orderAmountOrCountsByDate($storeId,$userId=0,$dateList=[],$isAmounts=1,$category=0){
		$where['store_id'] = $storeId;
		empty($category) ? : $where['category'] = 2;
		empty($userId) ? : $where['user_id'] = $userId;
		$model = $this->model
			->where($where)
			->where(['status'=>1]);
		$model = empty($dateList) ? $model->whereDate('created_at',date('Y-m-d')) :
			$model->whereDate('created_at','>=',$dateList['end_date'])
				->whereDate('created_at','<=',$dateList['start_date']);
		return $isAmounts ? $model->sum('receipts') : $model->count();
	}



}